﻿#region Using directives

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="VwAllCustomersProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract class VwAllCustomersProviderBaseCore : EntityViewProviderBase<VwAllCustomers>
	{
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions
		
		/*
		///<summary>
		/// Fill an VList&lt;VwAllCustomers&gt; From a DataSet
		///</summary>
		/// <param name="dataSet">the DataSet</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList&lt;VwAllCustomers&gt;"/></returns>
		protected static VList&lt;VwAllCustomers&gt; Fill(DataSet dataSet, VList<VwAllCustomers> rows, int start, int pagelen)
		{
			if (dataSet.Tables.Count == 1)
			{
				return Fill(dataSet.Tables[0], rows, start, pagelen);
			}
			else
			{
				return new VList<VwAllCustomers>();
			}	
		}
		
		
		///<summary>
		/// Fill an VList&lt;VwAllCustomers&gt; From a DataTable
		///</summary>
		/// <param name="dataTable">the DataTable that hold the data.</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList<VwAllCustomers>"/></returns>
		protected static VList&lt;VwAllCustomers&gt; Fill(DataTable dataTable, VList<VwAllCustomers> rows, int start, int pagelen)
		{
			int recordnum = 0;
			
			System.Collections.IEnumerator dataRows =  dataTable.Rows.GetEnumerator();
			
			while (dataRows.MoveNext() && (pagelen != 0))
			{
				if(recordnum >= start)
				{
					DataRow row = (DataRow)dataRows.Current;
				
					VwAllCustomers c = new VwAllCustomers();
					c.KhachHangId = (Convert.IsDBNull(row["KhachHangID"]))?Guid.Empty:(System.Guid)row["KhachHangID"];
					c.MaKh = (Convert.IsDBNull(row["MaKH"]))?string.Empty:(System.String)row["MaKH"];
					c.TenKh = (Convert.IsDBNull(row["TenKH"]))?string.Empty:(System.String)row["TenKH"];
					c.Level = (Convert.IsDBNull(row["LEVEL"]))?(int)0:(System.Int32)row["LEVEL"];
					c.ParentId = (Convert.IsDBNull(row["ParentID"]))?Guid.Empty:(System.Guid)row["ParentID"];
					c.BranchId = (Convert.IsDBNull(row["BranchID"]))?Guid.Empty:(System.Guid?)row["BranchID"];
					c.BranchCode = (Convert.IsDBNull(row["BranchCode"]))?string.Empty:(System.String)row["BranchCode"];
					c.Tel = (Convert.IsDBNull(row["Tel"]))?string.Empty:(System.String)row["Tel"];
					c.Address = (Convert.IsDBNull(row["Address"]))?string.Empty:(System.String)row["Address"];
					c.AcceptChanges();
					rows.Add(c);
					pagelen -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		*/	
						
		///<summary>
		/// Fill an <see cref="VList&lt;VwAllCustomers&gt;"/> From a DataReader.
		///</summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pageLength">number of row.</param>
		///<returns>a <see cref="VList&lt;VwAllCustomers&gt;"/></returns>
		protected VList<VwAllCustomers> Fill(IDataReader reader, VList<VwAllCustomers> rows, int start, int pageLength)
		{
			int recordnum = 0;
			while (reader.Read() && (pageLength != 0))
			{
				if(recordnum >= start)
				{
					VwAllCustomers entity = null;
					if (DataRepository.Provider.UseEntityFactory)
					{
						entity = EntityManager.CreateViewEntity<VwAllCustomers>("VwAllCustomers",  DataRepository.Provider.EntityCreationalFactoryType); 
					}
					else
					{
						entity = new VwAllCustomers();
					}
					entity.KhachHangId = (System.Guid)reader["KhachHangID"];
					//entity.KhachHangId = (Convert.IsDBNull(reader["KhachHangID"]))?Guid.Empty:(System.Guid)reader["KhachHangID"];
					entity.MaKh = (System.String)reader["MaKH"];
					//entity.MaKh = (Convert.IsDBNull(reader["MaKH"]))?string.Empty:(System.String)reader["MaKH"];
					entity.TenKh = reader.IsDBNull(reader.GetOrdinal("TenKH")) ? null : (System.String)reader["TenKH"];
					//entity.TenKh = (Convert.IsDBNull(reader["TenKH"]))?string.Empty:(System.String)reader["TenKH"];
					entity.Level = (System.Int32)reader["LEVEL"];
					//entity.Level = (Convert.IsDBNull(reader["LEVEL"]))?(int)0:(System.Int32)reader["LEVEL"];
					entity.ParentId = (System.Guid)reader["ParentID"];
					//entity.ParentId = (Convert.IsDBNull(reader["ParentID"]))?Guid.Empty:(System.Guid)reader["ParentID"];
					entity.BranchId = reader.IsDBNull(reader.GetOrdinal("BranchID")) ? null : (System.Guid?)reader["BranchID"];
					//entity.BranchId = (Convert.IsDBNull(reader["BranchID"]))?Guid.Empty:(System.Guid?)reader["BranchID"];
					entity.BranchCode = (System.String)reader["BranchCode"];
					//entity.BranchCode = (Convert.IsDBNull(reader["BranchCode"]))?string.Empty:(System.String)reader["BranchCode"];
					entity.Tel = reader.IsDBNull(reader.GetOrdinal("Tel")) ? null : (System.String)reader["Tel"];
					//entity.Tel = (Convert.IsDBNull(reader["Tel"]))?string.Empty:(System.String)reader["Tel"];
					entity.Address = reader.IsDBNull(reader.GetOrdinal("Address")) ? null : (System.String)reader["Address"];
					//entity.Address = (Convert.IsDBNull(reader["Address"]))?string.Empty:(System.String)reader["Address"];
					entity.AcceptChanges();
					rows.Add(entity);
					pageLength -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		
		
		/// <summary>
		/// Refreshes the <see cref="VwAllCustomers"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="VwAllCustomers"/> object to refresh.</param>
		protected void RefreshEntity(IDataReader reader, VwAllCustomers entity)
		{
			reader.Read();
			entity.KhachHangId = (System.Guid)reader["KhachHangID"];
			//entity.KhachHangId = (Convert.IsDBNull(reader["KhachHangID"]))?Guid.Empty:(System.Guid)reader["KhachHangID"];
			entity.MaKh = (System.String)reader["MaKH"];
			//entity.MaKh = (Convert.IsDBNull(reader["MaKH"]))?string.Empty:(System.String)reader["MaKH"];
			entity.TenKh = reader.IsDBNull(reader.GetOrdinal("TenKH")) ? null : (System.String)reader["TenKH"];
			//entity.TenKh = (Convert.IsDBNull(reader["TenKH"]))?string.Empty:(System.String)reader["TenKH"];
			entity.Level = (System.Int32)reader["LEVEL"];
			//entity.Level = (Convert.IsDBNull(reader["LEVEL"]))?(int)0:(System.Int32)reader["LEVEL"];
			entity.ParentId = (System.Guid)reader["ParentID"];
			//entity.ParentId = (Convert.IsDBNull(reader["ParentID"]))?Guid.Empty:(System.Guid)reader["ParentID"];
			entity.BranchId = reader.IsDBNull(reader.GetOrdinal("BranchID")) ? null : (System.Guid?)reader["BranchID"];
			//entity.BranchId = (Convert.IsDBNull(reader["BranchID"]))?Guid.Empty:(System.Guid?)reader["BranchID"];
			entity.BranchCode = (System.String)reader["BranchCode"];
			//entity.BranchCode = (Convert.IsDBNull(reader["BranchCode"]))?string.Empty:(System.String)reader["BranchCode"];
			entity.Tel = reader.IsDBNull(reader.GetOrdinal("Tel")) ? null : (System.String)reader["Tel"];
			//entity.Tel = (Convert.IsDBNull(reader["Tel"]))?string.Empty:(System.String)reader["Tel"];
			entity.Address = reader.IsDBNull(reader.GetOrdinal("Address")) ? null : (System.String)reader["Address"];
			//entity.Address = (Convert.IsDBNull(reader["Address"]))?string.Empty:(System.String)reader["Address"];
			reader.Close();
	
			entity.AcceptChanges();
		}
		
		/*
		/// <summary>
		/// Refreshes the <see cref="VwAllCustomers"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="VwAllCustomers"/> object.</param>
		protected static void RefreshEntity(DataSet dataSet, VwAllCustomers entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.KhachHangId = (Convert.IsDBNull(dataRow["KhachHangID"]))?Guid.Empty:(System.Guid)dataRow["KhachHangID"];
			entity.MaKh = (Convert.IsDBNull(dataRow["MaKH"]))?string.Empty:(System.String)dataRow["MaKH"];
			entity.TenKh = (Convert.IsDBNull(dataRow["TenKH"]))?string.Empty:(System.String)dataRow["TenKH"];
			entity.Level = (Convert.IsDBNull(dataRow["LEVEL"]))?(int)0:(System.Int32)dataRow["LEVEL"];
			entity.ParentId = (Convert.IsDBNull(dataRow["ParentID"]))?Guid.Empty:(System.Guid)dataRow["ParentID"];
			entity.BranchId = (Convert.IsDBNull(dataRow["BranchID"]))?Guid.Empty:(System.Guid?)dataRow["BranchID"];
			entity.BranchCode = (Convert.IsDBNull(dataRow["BranchCode"]))?string.Empty:(System.String)dataRow["BranchCode"];
			entity.Tel = (Convert.IsDBNull(dataRow["Tel"]))?string.Empty:(System.String)dataRow["Tel"];
			entity.Address = (Convert.IsDBNull(dataRow["Address"]))?string.Empty:(System.String)dataRow["Address"];
			entity.AcceptChanges();
		}
		*/
			
		#endregion Helper Functions
	}//end class

	#region VwAllCustomersFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VwAllCustomers"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VwAllCustomersFilterBuilder : SqlFilterBuilder<VwAllCustomersColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VwAllCustomersFilterBuilder class.
		/// </summary>
		public VwAllCustomersFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VwAllCustomersFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VwAllCustomersFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VwAllCustomersFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VwAllCustomersFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VwAllCustomersFilterBuilder

	#region VwAllCustomersParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VwAllCustomers"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VwAllCustomersParameterBuilder : ParameterizedSqlFilterBuilder<VwAllCustomersColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VwAllCustomersParameterBuilder class.
		/// </summary>
		public VwAllCustomersParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VwAllCustomersParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VwAllCustomersParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VwAllCustomersParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VwAllCustomersParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VwAllCustomersParameterBuilder
} // end namespace
